<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="90px">
          <el-form-item label="用户昵称：" prop="userName">
             <el-input
                v-model="queryParams.userName"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="用户ID：" prop="userId">
             <el-input
                v-model="queryParams.userId"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
             <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          </el-form-item>
       </el-form>
       <el-row :gutter="10" class="mb8"> 
            <el-col :span="1.5">
               <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd">添加群成员</el-button>
            </el-col>
       </el-row>
       <!-- 表格数据 -->
       <el-table v-loading="loading" :data="roleList" :default-sort="{ prop: 'roleLevel', order: 'descending' }">
         <el-table-column label="" prop="id" width="100" >
            <template #default="scope">
                <span class="idx"  :style="{ backgroundColor: scope.$index < 3 ? 'black' : 'gray' }">
                  {{scope.$index + 1}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="用户头像" prop="roleName" width="100">
            <template #default="scope">
              <span class="avatar" v-if="scope.row.faceURL === ''">
                  {{scope.row.nickname[0]}}
              </span>
              <div v-else>
                <img :src="scope.row.faceURL" alt="light" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="用户ID" prop="userID" width="150" />
          <el-table-column label="群员角色" prop="roleKey" width="100">
            <template #default="scope">
              <span>{{ getRole(scope.row) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="群呢称" prop="nickname" width="100"  />
          <el-table-column label="入群时间">
            <template #default="scope">
              <span>{{ parseTime(scope.row.joinTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入群方式" prop="joinSource">
               <template #default="scope">
                  <span>{{ getSource(scope.row.joinSource) }}</span>
               </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                 <el-button link type="primary"  @click="handleSet(scope.row)" :disabled="scope.row.roleLevel === 100">设置群身份</el-button>
                 <el-button link type="primary"  @click="handleCancelMute(scope.row)" v-if="scope.row.muteEndTime">取消禁言</el-button>
                 <el-button link type="primary"  @click="handleMute(scope.row)" v-else :disabled="scope.row.roleLevel === 100">禁言</el-button>
                 <el-button link type="primary"  @click="handleRemove(scope.row)" :disabled="scope.row.roleLevel === 100">移除</el-button>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          v-model:page="pageObj.pageNumber"
          v-model:limit="pageObj.showNumber"
          @pagination="getList"
       />
       <el-dialog :title="title" v-model="open" width="680px" append-to-body>
          <SelectUserDialog
              title=""
              :table-data="tableData"
              @submit="handleSubmitUsers"
              v-model="open"
              ref="selectUser"
          />
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </div>
          </template> 
        </el-dialog>
        <el-drawer size="20%" :title="title" v-model="openDrawer" direction="rtl" destroy-on-close>
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="0px">
             <el-form-item label="" prop="roleName" v-show="single">
                <div class="radioContainer">
                    <el-radio v-model="radio1" label="20">普通群员</el-radio>
                    <el-radio v-model="radio1" label="60">群管理员</el-radio>
                    <el-radio v-model="radio1" label="100">群主</el-radio>
                </div>
             </el-form-item>
          </el-form>
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="0px">
             <el-form-item label="" prop="roleName" v-show="!single">
                <div class="radioContainer">
                    <el-radio v-model="radio2" label="1">10分钟</el-radio>
                    <el-radio v-model="radio2" label="2">1小时</el-radio>
                    <el-radio v-model="radio2" label="3">12小时</el-radio>
                    <el-radio v-model="radio2" label="4">1天</el-radio>
                </div>
             </el-form-item>
             <div  class="customTime" v-show="!single">自定义时长</div>
             <el-form-item label="" prop="timer" v-show="!single">
                <el-input v-model="form.timer" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-drawer>
    </div>
 </template>
 
 <script setup name="Role">
 import { getGroupMember, modifyMemberAdmin, modifyManage, cancelManage, removeMember } from "@/api/im/group";
 import { onMounted, ref } from "vue";
 import SelectUserDialog from '@/components/SelectUserDialog'
 const route = useRoute();
 const { proxy } = getCurrentInstance();
 
 const roleList = ref([]);
 const loading = ref(true);
 const showSearch = ref(true);
 const single = ref(true);
 const total = ref(0);
 const title = ref("");
 const dateRange = ref([]);
 const open = ref(false);
 const openDrawer = ref(false);
 const radio1 = ref('20');
 const radio2 = ref('2');
 const groupID = ref('');
 const currentRow = ref({});
 const currentOwn = ref('');
 
 const data = reactive({
   form: {},
   queryParams: {
     pageNum: 1,
     pageSize: 10,
     userName: undefined,
     userId: undefined,
   },
   pageObj:{
      pageNumber: 1,
      showNumber: 10
   },
   rules: {
     roleName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
     iphone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
     password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
   },
 });
 
 const { queryParams, form, rules, pageObj } = toRefs(data);
onMounted(() => { 
   groupID.value = route.params.groupID
   getList();
});
 /** 查询角色列表 */
 function getList() {
   loading.value = true;
   const params = {
      pagination: pageObj.value,
      groupID: groupID.value
   }
   getGroupMember(params).then(response => {
     roleList.value = response.data.members;
     total.value = response.data.total;
     loading.value = false;
     if(isArray(response.data.members)){
         response.data.members.filter(item => {
            if(item.roleLevel === 100){
               currentOwn = item.userID
            }
         })
     }
   });
 }
 /* 获取用户角色 */
 function getRole (row){
   switch(row.roleLevel){
      case 100: 
        return '群主'
      case 60:
        return '管理员'
      case 20 :
        return '普通成员'
   }
 }
 /* 获取入群方式 */
 function getSource (source){
   switch(source){
      case 2: 
        return '邀请入群'
      case 1:
        return '扫码'
      case 3 :
        return '搜索'
   }
 }
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
 }
 function handleAdd () {
    open.value = true
 }
 function handleSet(row) {
   radio1.value = row.roleLevel.toString();
   openDrawer.value = true;
   title.value = '设置群身份';
   single.value = true;
   currentRow.value = row;
 }
 function handleMute (record) {
    openDrawer.value = true;
    title.value = '禁言';
    single.value = false;
 }
 function submitForm (){
   switch(single.value){
      case true:
         changeRole()
      break;
      case false:
         changeMute()
      break;
   }
 }
 function changeRole (){
   let params;
   if(radio1.value !==  '100'){
      params = {
         members:{...currentRow.value, roleLevel: radio1},
      }
      modifyManage(params).then((res=>{
         openDrawer.value = false;
         getList();
      }))
   }else{
      params = {
         groupID: currentRow.value.groupID,
         newOwnerUserID: currentRow.value.userID,
         oldOwnerUserID: currentOwn.value,
      }
      modifyMemberAdmin(params).then((res=>{
         openDrawer.value = false;
         getList();
      }))
   }
 }
 function handleCancelMute (row){
   const params = {
      groupID: row.groupID,
      userID: row.userID
   }
   proxy.$modal.confirm('确定要取消禁言吗？').then(function () {
     return cancelManage(params);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("解除成功");
   }).catch(() => {});
 }
 function handleRemove (row){
   const parmas = {
      groupID: row.groupID,
      kickedUserIDs: [row.userID],
      reason:''
   }
   proxy.$modal.confirm('确定要移除该用户吗？').then(function () {
     return removeMember(parmas);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("移除成功");
   }).catch(() => {});
 } 
 </script>
<style scoped>
.upload_c{
    margin-bottom: 10px;
}
.upload_text{
    text-align: center;
    margin: 0 auto;
    margin-top: 10px;
    color: #1890ff;
    font-size: 14px;
}
.upload{
    width: 53px;
    height: 53px;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
    margin: 0 auto;
}
.radioContainer{
    display: flex;
    flex-direction: column;
}
.customTime{
    margin-bottom: 10px;
    font-weight: 600;
}
</style>
 